package com.renbang.action.user;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.renbang.dao.sqlsession.DBUtils;
import com.renbang.dao.sqlsession.DaoImp;
import com.renbang.model.sys.SysFun;
import com.renbang.model.sys.SysPower;
import com.renbang.model.sys.SysUser;
import com.renbang.service.sys.SysFunServiceImp;
import com.renbang.service.sys.SysLogServiceImp;
import com.renbang.service.sys.SysPowerServiceImp;


@Controller
@RequestMapping(value = "/user")
public class UserAction{
	@Value("#{dataSource['url']}")
	private String URL;
	@Value("#{dataSource['driverClassName']}")
	private String DRIVER;
	@Value("#{dataSource['username']}")
	private String USER;
	@Value("#{dataSource['password']}")
	private String PASS;
	@Autowired
	SysLogServiceImp sysLogServiceImp;
	@Autowired
	SysPowerServiceImp sysPowerServiceImp;
	@Autowired
	SysFunServiceImp sysFunServiceImp;
	@RequestMapping(value = "/{page}")
	public String redirect(@PathVariable("page") String page) {
		if(DBUtils.DRIVER==null||DBUtils.DRIVER.equals("")){
			DBUtils.setConnection(DRIVER, URL, USER, PASS);
		}
		return "user/" + page;
	}
	@RequestMapping(value = "/{page1}/{page2}")
	public String redirect(@PathVariable("page1") String page1,@PathVariable("page2") String page2) {
		return "user/" + page1+"/"+page2;
	}
	@RequestMapping(value = "/fun/myfun")
	public @ResponseBody List<SysFun> all(HttpServletRequest request) {
		SysUser sysUser = (SysUser) request.getSession().getAttribute("user");//拿到登录得user对象
		SysPower syspower = new SysPower();
		syspower.setUserId(sysUser.getUserId());
		//通过用户id查出权限表与之对应得功能id集合
		List<Integer> funIds =sysPowerServiceImp.findFunId(syspower);
		List<SysFun> funList = new ArrayList<SysFun>();
		for (Integer funid : funIds) {
			SysFun sysfun = sysFunServiceImp.findById(funid);
			funList.add(sysfun);
		}
		return 	funList;
	}

}